using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

namespace TcKs.MSOffice.WrapperAssembliesCodeGenerator {
	public class ProjectGenerator {
		private CodeGenerator codeGenerator = new CodeGenerator();
		public CodeGenerator CodeGenerator {
			get { return this.codeGenerator; }
			set { this.codeGenerator = value; }
		}

		private string targetDirectory;
		public string TargetDirectory {
			get { return this.targetDirectory; }
			set { this.targetDirectory = value; }
		}

		public void GenerateTypesFromTypeMap( Assembly modelAssembly ) {
			Type[] arr_Types = modelAssembly.GetTypes();

			int counter = 0;
			foreach ( Type tpModel in arr_Types ) {
				//if ( false == tpModel.IsEnum ) { continue; }
				if ( false == this.CodeGenerator.Context.TypeMap.ContainsKey( tpModel ) ) { continue; }

				counter++;
				Console.Write( "{0} - {1} ... ", counter, tpModel.Name );

				string fileName = tpModel.Name + ".vb";
				string filePath = System.IO.Path.Combine( this.TargetDirectory, fileName );

				StringBuilder sbCode = new StringBuilder();
				VbNetSourceCodeGenerator coder = new VbNetSourceCodeGenerator( sbCode );
				this.codeGenerator.GenerateCodeForType( tpModel, coder );
				coder.Flush();

				try {
					using ( System.IO.FileStream fstrm = new System.IO.FileStream( filePath, System.IO.FileMode.Create, System.IO.FileAccess.Write ) ) {
						using ( System.IO.StreamWriter swr = new System.IO.StreamWriter( fstrm ) ) {
							swr.Write( sbCode );
						}
					}

					Console.ForegroundColor = ConsoleColor.Green;
					Console.WriteLine( "OK" );
					Console.ResetColor();
				}
				catch ( System.UnauthorizedAccessException exc ) {
					Console.ForegroundColor = ConsoleColor.Red;
					Console.WriteLine( "ACCESS DENIED" );
					Console.ResetColor();
				}
			}
		}
		public void GenerateEnumTypes( Assembly modelAssembly ) {
			Type[] arr_Types = modelAssembly.GetTypes();

			int counter = 0;
			foreach ( Type tpModel in arr_Types ) {
				if ( false == tpModel.IsEnum ) { continue; }

				counter++;
				Console.Write( "{0} - {1} ... ", counter, tpModel.Name );

				string fileName = tpModel.Name + ".vb";
				string filePath = System.IO.Path.Combine( this.TargetDirectory, fileName );

				StringBuilder sbCode = new StringBuilder();
				VbNetSourceCodeGenerator coder = new VbNetSourceCodeGenerator( sbCode );
				this.codeGenerator.GenerateCodeForType( tpModel, coder );
				coder.Flush();

				try {
					using ( System.IO.FileStream fstrm = new System.IO.FileStream( filePath, System.IO.FileMode.Create, System.IO.FileAccess.Write ) ) {
						using ( System.IO.StreamWriter swr = new System.IO.StreamWriter( fstrm ) ) {
							swr.Write( sbCode );
						}
					}

					Console.ForegroundColor = ConsoleColor.Green;
					Console.WriteLine( "OK" );
					Console.ResetColor();
				}
				catch ( System.UnauthorizedAccessException exc ) {
					Console.ForegroundColor = ConsoleColor.Red;
					Console.WriteLine( "ACCESS DENIED" );
					Console.ResetColor();
				}
			}
		}
	}
}
